﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BookShop;
using SubSonic;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //GridView1.DataSource = Book.FetchAll();
        //GridView1.DataBind();
        //if (!Page.IsPostBack)
        //{
        //    Class2 cl = new Class2();
        //    GridView1.DataSource = cl.GetBookInfo();
        //    GridView1.DataBind();
        //}
        //Book book = new Book();
        //SqlQuery q = new Select(Book.IsbnColumn, Book.BookNameColumn, Book.AuthorColumn, Book.PublisherColumn, Book.PriceColumn, Category.NameColumn)
        //        .From<Book>().InnerJoin(Category.CategoryIDColumn, Book.CategoryIDColumn).Paged(1,10);
        //Response.Write(q.ToString());
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //ObjectDataSource1.DataBind();
    }

    protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
    {
        GridViewRow pagerRow = GridView1.BottomPagerRow;
        DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        GridView1.PageIndex = pageList.SelectedIndex;
    }

    protected void GridView1_DataBound(Object sender, EventArgs e)
    {
       
    }

    protected void GridView1_DataBound1(object sender, EventArgs e)
    {
        GridViewRow pagerRow = GridView1.BottomPagerRow;
        LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
        LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
        LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
        LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");
        if (GridView1.PageIndex == 0)
        {
            linkBtnFirst.Enabled = false;
            linkBtnPrev.Enabled = false;
        }
        else if (GridView1.PageIndex == GridView1.PageCount - 1)
        {
            linkBtnLast.Enabled = false;
            linkBtnNext.Enabled = false;
        }
        else if (GridView1.PageCount <= 0)
        {
            linkBtnFirst.Enabled = false;
            linkBtnPrev.Enabled = false;
            linkBtnNext.Enabled = false;
            linkBtnLast.Enabled = false;
        }
        DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
        if (pageList != null)
        {
            for (int i = 0; i < GridView1.PageCount; i++)
            {
                int pageNumber = i + 1;
                ListItem item = new ListItem(pageNumber.ToString() + "/" + GridView1.PageCount.ToString(), pageNumber.ToString());
                if (i == GridView1.PageIndex)
                {
                    item.Selected = true;
                }
                pageList.Items.Add(item);
            }
        }
        if (pageLabel != null)
        {
            int currentPage = GridView1.PageIndex + 1;
            pageLabel.Text = "当前页： " + currentPage.ToString() +
              " / " + GridView1.PageCount.ToString();
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // 在 GridView 控件中按下"选取"按钮来选取某一列数据之后，
        // 就将 DetailsView 控件切换至只读模式，也就是一般的显示模式。
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
    }

    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        if (e.Exception != null || e.AffectedRows <= 0)
        {
            ErrorMessageLabel.Text = "在添加数据时发生错误，请确认所输入之数据的格式是否正确。";
            e.ExceptionHandled = true;
        }
        else
        {
            ErrorMessageLabel.Text = "已经成功添加资料...";
        }

        GridView1.DataBind();
        this.UpdatePanel1.Update();
    }

    protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.Exception != null || e.AffectedRows <= 0)
        {
            ErrorMessageLabel.Text = "在编辑数据时发生错误，请确认所输入之数据的格式是否正确。";
            e.ExceptionHandled = true;
        }
        else
        {
            ErrorMessageLabel.Text = "已经成功编辑数据...";
        }

        GridView1.DataBind();
        this.UpdatePanel1.Update();
    }
}
